import config from '../../config.js';
const base = config;
var app = getApp();

Page({
  data: {
    tabs: [],
    activeIndex: 0,
    sliderOffset: 0,
    orderId: "",
    // 订单
    orderList: [],
    content: "",
    width: 94,
    type: null,
    tag: [{
      text: "服务态度好",
      value: "0",
      checked: false,
    }, {
      text: "服务质量高",
      value: "1",
      checked: false,
    }, {
      text: "服务及时",
      value: "2",
      checked: false,
    }, {
      text: "专业性高",
      value: "3",
      checked: false,
    }],

    exp: [{
      text: "车内整洁干净",
      value: "0",
      checked: false,
    }, {
      text: "收费合理",
      value: "1",
      checked: false,
    }],

    evaShow: false,
    page: {
      start: 0,
      size: 10,
      isEnd: false
    },

  },

  evaFun: function(e) {

    var orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: './orderComment?orderid=' + orderId,
    })
  },

  closeEva: function() {
    this.setData({
      evaShow: false
    })
  },

  stopMp: function() {

  },

  tagSelect: function(e) {
    var tagArr = this.data.tag;
    var checkArr = e.detail.value;
    for (var i = 0; i < tagArr.length; i++) {
      if (checkArr.indexOf(i + "") != -1) {
        tagArr[i].checked = true;
      } else {
        tagArr[i].checked = false;
      }
    }
    this.setData({
      tag: tagArr
    })
  },

  expSelect: function(e) {
    var tagArr = this.data.exp;
    var checkArr = e.detail.value;
    for (var i = 0; i < tagArr.length; i++) {
      if (checkArr.indexOf(i + "") != -1) {
        tagArr[i].checked = true;
      } else {
        tagArr[i].checked = false;
      }
    }
    this.setData({
      exp: tagArr
    })
  },


  star: function(e) {
    //console.log(e.currentTarget.offsetLeft);
    console.log(e.detail.x);
    let width = e.detail.x;
    this.setData({
      width: width,
    })
  },
  loadData(index) {
    var that = this;
    index = that.data.activeIndex;

    //请求订单列表
    wx.request({
      url: base.orderListUrl,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        start: that.data.page.start,
        length: that.data.page.size,
        search: '',
        type: that.data.tabs[index].index
      },
      method: "POST",
      success: function(res) {
        if (res.data.status == 200) {
          if (that.data.page.start != 0) {
            if (res.data.data != null && res.data.data.length != 0) {
              var arr = that.data.orderList;
              for (var i = 0; i < res.data.data.length; i++) {
                arr.push(res.data.data[i])
              }

              that.setData({
                orderList: arr
              })
            } else {
              that.setData({
                'page.isEnd': true
              })
            }
          } else {
            // console.log(res.data);
            that.setData({
              orderList: res.data.data
            });
          }



        } else {
          wx.showToast({
            title: '订单获取失败！',
            icon: 'none',
            duration: 1500
          })
          return false;
        }

        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
      }

    })
  },
  reModifyDetail(e) {
    var that = this;
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "订单修改确认",
      content: "请确认是否接受管家修改订单！",
      showCancel: true,
      confirmColor: "#137AF0",
      success: function(e) {
        console.log(e);
        if (e.confirm) {
          //取消订单
          wx.request({
            url: base.orderReConfirm,
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Cookie': wx.getStorageSync("session")
            },

            data: {
              id: id,
              content: "",
              from: 0
            },
            method: "POST",
            success: function(res) {
              console.log(res.data);
              if (res.data.status == 200) {
                that.loadNoPay();
                console.log(that);
              } else {
                wx.showToast({
                  title: '订单确认失败！',
                  icon: 'none',
                  duration: 1500
                })
                return false;
              }
            }
          });
        }
      }
    })
  },
  loadTabs: function(index) {
    var that = this;
    var type = wx.getStorageSync("mineOrders");
    var tabs = config.tab1;

    if (index == 2) {
      if (type != null) {
        tabs = config.tab3;
        wx.setStorageSync("mineOrders", null); //清除
      } else {
        tabs = config.tab2;
      }
    }

    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          //tabs: that.loadTabs(0),
          slideWidth: res.windowWidth / tabs.length,
          sliderOffset: res.windowWidth / tabs.length * that.data.activeIndex,
        });
      }
    });

    return tabs;
  },
  loadNoPay: function(index) {
    //请求管家列表
    var that = this;
    wx.request({
      url: base.orderNoPay,
      header: {
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: index
      },
      method: "POST",
      success: function(res) {
        console.log(res.data.data);
        if (res.data.data.length > 0) {
          var tabs = that.loadTabs(2);

        } else {
          var tabs = that.loadTabs(1);

        }


        that.setData({
          tabs: tabs,
        })

        that.loadData(index);
      }
    })
  },
  onLoad: function(options) {
    console.log(options);

    var that = this;


    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          //tabs: that.loadTabs(0),
          slideWidth: res.windowWidth / that.data.tabs.length,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex,
        });
      }
    });



  },
  onShow: function(e) {
    var that = this;
    var type = wx.getStorageSync("orderConfirmType");
    console.log(type);
    if (type != null && type == 1) {
      var orderId = wx.getStorageSync("orderId");
      this.data.orderId = orderId;
      if (orderId) {
        wx.navigateTo({
          url: "../orderDetail/orderDetail?id=" + orderId,
          complete: function() {
            wx.setStorageSync("orderConfirmType", null);
            wx.setStorageSync("orderId", null);
          }
        })
      }
    } else {
      wx.startPullDownRefresh({
        success: function() {
          wx.showNavigationBarLoading();

        }
      })

      //清除消息
      wx.removeTabBarBadge({
        index: 2,
      })
    }


    this.listenOrderChange();
  },

  roderDetail: function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../orderDetail/orderDetail?id=" + id,
    })
  },
  roderCancel: function(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "取消订单确认",
      content: "请确认是否取消订单，取消订单后您可以再次下单！",
      showCancel: true,
      confirmColor: "#137AF0",
      success: function(e) {
        console.log(e);
        if (e.confirm) {
          //取消订单
          wx.request({
            url: base.orderCancelUrl,
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Cookie': wx.getStorageSync("session")
            },

            data: {
              id: id,
              content: "",
              from: 0
            },
            method: "POST",
            success: function(res) {
              console.log(res.data);
              if (res.data.status == 200) {
                that.loadNoPay();
                console.log(that);
              } else {
                wx.showToast({
                  title: '订单取消失败！',
                  icon: 'none',
                  duration: 1500
                })
                return false;
              }
            }
          });
        }
      }
    })

  },
  tabClick: function(e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    this.loadData(0);
  },
  roderOrder: function(e) {
    var oid = e.currentTarget.dataset.id;
    console.log(oid);
    var typeId = e.currentTarget.dataset.ordertype.typeId;
    var carid = e.currentTarget.dataset.cid;
    console.log("carid:" + carid);
    var url = '';
    switch (typeId) {
      case 1:
        //车险报价
        url = '../insurance/step2';
        break;
      case 2:
        //事故协助
        url = '../accidentAssi/step2';
        break;
      case 3:
        //违章办理
        url = '../peccancyList/peccancyList';
        break;
      case 4:
        //代维修
        url = '../repair/repair';
        break;
      case 5:
        //代保养
        url = '../maintenance/step2';
        break;
      case 6:
        //接送机
        url = '../airport/step2';
        break;
      case 7:
        //代步车
        url = '../stepcar/stepCarService';
        break;
      case 8:
        //救援服务
        url = '../insurance/insurance';
        break;
      case 9:
        //代年检
        url = '../yearaudit/yearauditService';
        break;
      default:
        {
          wx.showModal({
            title: "温馨提示",
            content: "请您直接在首页下单！",
            showCancel: false,
            confirmColor: "#137AF0",
            success: function(e) {

            }
          })
        }
        break;
    }

    url += '?orderId=' + oid;
    if (typeId == 3) {
      url += "&id=" + carid;
    }

    wx.navigateTo({
      url: url,
    })

  },
  moreAction: function() {
    if (!this.data.page.isEnd) {
      this.setData({
        'page.start': ++this.data.page.start,

      })
      this.loadData();
    }

  },
  onPullDownRefresh: function() {
    var that = this;
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.setData({
      'page.start': 0,
      'page.isEnd': false
    })
    var index = that.data.activeIndex;
    that.loadNoPay(index);
  },
  listenOrderChange: function() {
    var that = this;
    app.globalData.callback = function(res) {
      console.log('收到服务器推送订单消息内容：' + res.data);
      //解析消息 
      that.loadNoPay();
    }
  },
  //行驶证
  uploadFr: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          licenseFr: tempFilePaths,
          isUploadFr: true,
          isUploadFirst: false
        })

        console.log(that.data.licenseFr);


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: openIdUrl.uploadPic,
          filePath: that.data.licenseFr[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            res = JSON.parse(res.data);
            console.log(res);
            if (res.status == 200) {
              that.setData({
                isUploadFr: true,
              })
              that.data.licenseFr = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },

  uploadBa: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          licenseBa: tempFilePaths,
          isUploadBa: true,
          isUploadBaFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: openIdUrl.uploadPic,
          filePath: that.data.licenseBa[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            console.log(res);
            res = JSON.parse(res.data);
            if (res.status == 200) {
              that.setData({
                isUploadBa: true,
              })
              that.data.licenseBa = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },
  //驾驶证正面
  uploadFrID: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          idenCardA: tempFilePaths,
          isUploadIDFr: true,
          isUploadIDFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: openIdUrl.uploadPic,
          filePath: that.data.idenCardA[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            res = JSON.parse(res.data);
            console.log(res);
            if (res.status == 200) {
              that.setData({
                isUploadIDFr: true,
              })
              that.data.idenCardA = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },

  uploadBaID: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          idenCardB: tempFilePaths,
          isUploadIDBa: true,
          isUploadBaIDFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: openIdUrl.uploadPic,
          filePath: that.data.idenCardB[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            console.log(res);
            res = JSON.parse(res.data);
            if (res.status == 200) {
              that.setData({
                isUploadIDBa: true,
              })
              that.data.idenCardB = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },
});